package com.herionthenet;

import java.nio.ByteBuffer;
import java.util.List;

public abstract class FrameDecoder {

	public enum FramerDataOption {
		TEXT, BINARY
	}
	
	protected byte[] delimiters;
	private FramerDataOption framerDataOption;
	
	public FrameDecoder(byte[] delimiter, FramerDataOption option) {
		if(delimiter == null || delimiter.length == 0)
			throw new IllegalArgumentException("Invalid frame delimiter");
		delimiters = delimiter;
		framerDataOption = option;
	}	

	protected boolean endOfFrame(byte[] array, int positionAt) {
				
		if(positionAt < delimiters.length)
			return false;	
		
		int j = 0;
		for(int i = positionAt - (delimiters.length - 1); i <= positionAt; i++){
			byte compare_byte_at = array[i];
			byte delimiter_byte_at = delimiters[j];
			
			if(compare_byte_at == delimiter_byte_at){
				j++;
				continue;
			}else
				return false;					
		}		
		
		return true;
	}

	public abstract List<byte[]> decode(ByteBuffer buffer);	
	
	public FramerDataOption getFramerDataOption(){
		return framerDataOption;
	}
	
}
